#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
翻译补齐工具 - Pydantic模型定义
"""

from typing import List, Optional
from pydantic import BaseModel, Field, validator


class TranslationItem(BaseModel):
    """单个翻译项模型"""
    key: str = Field(description="翻译键名")
    cn: Optional[str] = Field(default=None, description="中文翻译")
    en: Optional[str] = Field(default=None, description="英文翻译")
    tr: Optional[str] = Field(default=None, description="土耳其语翻译")

    @validator('cn', 'en', 'tr')
    def validate_translation(cls, v):
        """验证翻译内容不为空字符串"""
        if v == "":
            return None
        return v

    def has_duplicate_translations(self) -> bool:
        """检查是否存在重复的翻译值"""
        translations = [t for t in [self.cn, self.en, self.tr] if t is not None and t.strip()]
        return len(translations) != len(set(translations))


class TranslationBatch(BaseModel):
    """批量翻译模型"""
    translations: List[TranslationItem] = Field(description="翻译项目列表")

    def get_valid_items(self) -> List[TranslationItem]:
        """获取有效的翻译项（没有重复翻译的项）"""
        return [item for item in self.translations if not item.has_duplicate_translations()]

    def get_invalid_items(self) -> List[TranslationItem]:
        """获取无效的翻译项（有重复翻译的项）"""
        return [item for item in self.translations if item.has_duplicate_translations()]